﻿//-----------------------------------------------------------------------
// <copyright file="Imaging.cs" author="Lennart">
//     License: MIT
// </copyright>
//-----------------------------------------------------------------------
namespace UtilityMethods {
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.IO;
    using System.Windows.Forms;

    /// <summary>
    /// Provides Imaging-related functions.
    /// </summary>
    public static class Imaging {
        /// <summary>
        /// Converts a PNG image file to an icon file.
        /// </summary>
        /// <param name="imageFile">The path including filename to the image file.</param>
        /// <returns>The full path to the converted Icon.</returns>
        public static string IcoFromImageFile(string imageFile) {
            string newIconFile = Application.StartupPath + "\\" + Path.GetFileNameWithoutExtension(imageFile) + ".ico";
            Bitmap newIcon = ResizeImage(256, 256, Image.FromFile(imageFile));
            SaveAsIcon(newIcon, newIconFile);
            return newIconFile;
        }

        /// <summary>
        /// Resizes an Image and returns a Bitmap.
        /// </summary>
        /// <param name="width">New width of the image.</param>
        /// <param name="height">New height of the image.</param>
        /// <param name="sourceImage">The source image.</param>
        /// <returns>A resized Bitmap.</returns>
        public static Bitmap ResizeImage(int width, int height, Image sourceImage) {
            Bitmap resizedImage = new Bitmap(width, height);
            using (Graphics imageOptions = Graphics.FromImage(resizedImage)) {
                imageOptions.InterpolationMode = InterpolationMode.HighQualityBicubic;
                imageOptions.PixelOffsetMode = PixelOffsetMode.HighQuality;
                imageOptions.SmoothingMode = SmoothingMode.HighQuality;
                imageOptions.DrawImage(sourceImage, new Rectangle(0, 0, width, height));
            }

            return resizedImage;
        }

        /// <summary>
        /// Creates an *.ico file from a PNG image.
        /// </summary>
        /// <param name="sourceBitmap">The PNG image.</param>
        /// <param name="filePath">The path where the icon is saved.</param>
        public static void SaveAsIcon(Bitmap sourceBitmap, string filePath) {
            using (MemoryStream imageStream = new MemoryStream()) {
                sourceBitmap.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);

                byte[] iconHeader = new byte[] {
                    // Icon Header
                    0, 0, // Reserved, always 0
                    1, 0, // 1 for *.ico, 0 for *.cur
                    1, 0, // Number of images in the file
                    // Image Size; 0 = 256px
                    0, 0,

                    // Number of colors in the palette, 0 if not using one
                    0,

                    // Reserved, probably ignored anyway
                    0,

                    // Number of color planes
                    1, 0,

                    // Bits per pixel
                    32, 0,

                    // Size of the PNG image, must be shifted depending on length
                    (byte)imageStream.Length,
                    (byte)(imageStream.Length >> 8),
                    (byte)(imageStream.Length >> 16),
                    (byte)(imageStream.Length >> 24),

                    // Offset where the image data starts, fixed at 22 bytes
                    22, 0, 0, 0 
                };

                // Concatenate header and image data and write the *.ico file
                using (FileStream iconStream = new FileStream(filePath, FileMode.Create)) {
                    iconStream.Write(iconHeader, 0, iconHeader.Length);
                    iconStream.Write(imageStream.ToArray(), 0, (int)imageStream.Length);
                }
            }
        }
    }
}
